
//
// 110118 - AH - (Bien *) Created.
// 110130 - AH - (Bien) Now shows RetrievalEvent property turn if is not -1.
// 110603 - AH - (Garage) Got color rendering of PAINT events to work and 
//               did italic rendering of PASS events.
// 110605 - AH - (Garage) HTML rendering of new event types LEVEL and TURN.
// 110717 - AH - (Bien) Removed not used code.
//

package org.soekrat.gui.swing.retrieval;

import java.awt.Color;
import java.awt.Component;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JLabel;
import javax.swing.JTree;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.TreeCellRenderer;

import org.soekrat.engine.wheel.retrieval.RetrievalEvent;
import org.soekrat.engine.wheel.retrieval.RetrievalEventType;

/**
 * <p>
 *   {@link TreeCellRenderer} for
 *   {@link RetrievalEvent} objects.
 * </p>
 * @author Arne Halvorsen (AH)
 * @author Laetitia Le Chatton (LLC) 
 */
public final class RetrievalEventCellRenderer implements TreeCellRenderer 
{
  /**
   * <p>
   *   Single instance shared.
   * </p>
   */
  public static RetrievalEventCellRenderer INSTANCE=
    new RetrievalEventCellRenderer();
  
  private final DateFormat m_df=new SimpleDateFormat();
  
  private final DefaultTreeCellRenderer m_dtcr=new DefaultTreeCellRenderer();
  
  // Singleton dictates private constructor.
  private RetrievalEventCellRenderer()
  {
    m_dtcr.setOpaque(true);
  }

  // Implemented in TreeCellRenderer.
  public Component getTreeCellRendererComponent(JTree tree, Object value,
    boolean selected, boolean expanded, boolean leaf, int row, boolean hasFocus) 
  {
    String text=value.toString();
    
    if (value instanceof RetrievalEvent)
    {
      RetrievalEvent re=(RetrievalEvent)value;
      
      if (re.getPrevious()==null)
      {
        long t=re.getTime();
        text=text+" ("+m_df.format(new Date(t))+")";
      }
      
      JLabel l=(JLabel)m_dtcr.getTreeCellRendererComponent(tree, text, selected, 
        expanded, leaf, row, hasFocus);
      
      RetrievalEventType type=re.getType();
      
      if (type==RetrievalEventType.PAINT)
      {
        l.setBackground(Color.GREEN.brighter().brighter());
      }
      else
      {
        l.setBackground(Color.WHITE);
      }
      
      switch (type)
      {
        case PASS :
          l.setText("<html><i>"+text+"</i></html>");
        break;
        
        case LEVEL :
          l.setText("<html><u>"+text+"</u></html>");
        break;
        
        case TURN :
          l.setText("<html><b>"+text+"</b></html>");
        break;
      }
      
      return l;
    }
    else
    {
      return m_dtcr.getTreeCellRendererComponent(tree, text, selected, expanded, 
        leaf, row, hasFocus);
    }
  }
  
}
